import '../justfiles/prerequisites.just'

KURTOSIS_PACKAGE := "./optimism-package-trampoline/"

test: _prerequisites
    go test --tags=testonly ./...

_kurtosis-run PACKAGE_NAME ARG_FILE ENCLAVE:
	kurtosis run {{PACKAGE_NAME}} --args-file {{ARG_FILE}} --enclave {{ENCLAVE}} --show-enclave-inspect=false --image-download=missing

_prestate-build PATH='.':
    docker buildx build --output {{PATH}} --progress plain -f ../op-program/Dockerfile.repro ../

_docker_build TAG TARGET CONTEXT DOCKERFILE *ARGS: _prerequisites
    #!/usr/bin/env bash
    # --load is needed to ensure the image ends up in the local registry
    # --provenance=false is needed to make the build idempotent
    docker buildx build \
        --load \
        --provenance=false \
        -t {{TAG}} \
        -f {{CONTEXT}}/{{DOCKERFILE}} \
        {{ if TARGET != '' {  "--target " + TARGET } else { "" } }} \
        --build-arg GIT_COMMIT={git_commit} \
        --build-arg GIT_DATE={git_date} \
        {{ ARGS }} \
        {{CONTEXT}}

_docker_build_stack TAG TARGET *ARGS: (_docker_build TAG TARGET "../" "ops/docker/op-stack-go/Dockerfile" ARGS)

cannon-image TAG='cannon:devnet': (_docker_build_stack TAG "cannon-target")
da-server-image TAG='da-server:devnet': (_docker_build_stack TAG "da-server-target")
op-batcher-image TAG='op-batcher:devnet': (_docker_build_stack TAG "op-batcher-target")
# TODO: this is a temporary hack to get the kona + asterisc version right.
# Ideally the Dockerfile should be self-sufficient (right now we depend on
# docker-bake.hcl to do the right thing).
op-challenger-image TAG='op-challenger:devnet': (_docker_build_stack TAG "op-challenger-target" "--build-arg" "KONA_VERSION=1.0.1" "--build-arg" "ASTERISC_VERSION=v1.3.0")
op-conductor-image TAG='op-conductor:devnet': (_docker_build_stack TAG "op-conductor-target")
op-deployer-image TAG='op-deployer:devnet': (_docker_build_stack TAG "op-deployer-target")
op-dispute-mon-image TAG='op-dispute-mon:devnet': (_docker_build_stack TAG "op-dispute-mon-target")
op-node-image TAG='op-node:devnet': (_docker_build_stack TAG "op-node-target")
op-program-image TAG='op-program:devnet': (_docker_build_stack TAG "op-program-target")
op-proposer-image TAG='op-proposer:devnet': (_docker_build_stack TAG "op-proposer-target")
op-supervisor-image TAG='op-supervisor:devnet': (_docker_build_stack TAG "op-supervisor-target")
op-wheel-image TAG='op-wheel:devnet': (_docker_build_stack TAG "op-wheel-target")
op-faucet-image TAG='op-faucet:devnet': (_docker_build_stack TAG "op-faucet-target")
op-interop-mon-image TAG='op-interop-mon:devnet': (_docker_build_stack TAG "op-interop-mon-target")

op-program-builder-image TAG='op-program-builder:devnet': _prerequisites
    just op-program-svc/op-program-svc {{TAG}}


# Devnet template recipe
devnet TEMPLATE_FILE DATA_FILE="" NAME="" PACKAGE=KURTOSIS_PACKAGE: _prerequisites
    #!/usr/bin/env bash
    export DEVNET_NAME={{NAME}}
    if [ -z "{{NAME}}" ]; then
        export DEVNET_NAME=`basename {{TEMPLATE_FILE}} .yaml`
        if [ -n "{{DATA_FILE}}" ]; then
            export DATA_FILE_NAME=`basename {{DATA_FILE}} .json`
            export DEVNET_NAME="$DEVNET_NAME-$DATA_FILE_NAME"
        fi
    fi
    export ENCL_NAME="$DEVNET_NAME"-devnet
    export CONDUCTOR_CONFIG="tests/op-conductor-ops-$ENCL_NAME.toml"
    go run cmd/main.go -kurtosis-package {{PACKAGE}} \
        -environment "tests/$ENCL_NAME.json" \
        -conductor-config "$CONDUCTOR_CONFIG" \
        -template "{{TEMPLATE_FILE}}" \
        -data "{{DATA_FILE}}" \
        -enclave "$ENCL_NAME" \
    && cat "tests/$ENCL_NAME.json" && if [ -f "$CONDUCTOR_CONFIG" ]; then cat "$CONDUCTOR_CONFIG"; fi

devnet-test DEVNET *TEST: _prerequisites
    #!/usr/bin/env bash
    export TESTS=({{TEST}})
    # we need a timestamp in there to force kurtosis to not cache the test solely based on its name!
    export ARGS=$(printf '%s\n' "${TESTS[@]}" | jq -R . | jq -s . | jq -s '{devnet: "{{DEVNET}}", timestamp: "{{datetime("%s")}}", tests: add}')
    kurtosis run --enclave {{DEVNET}} \
        --show-enclave-inspect=false \
        ./tests/ "$ARGS"

# Devnet recipes

# Simple devnet
simple-devnet: (devnet "simple.yaml")

# Interop devnet
interop-devnet: (devnet "interop.yaml")
interop-devnet-test: (devnet-test "interop-devnet" "interop-smoke-test.sh")

# User devnet
user-devnet DATA_FILE:
    {{just_executable()}} devnet "user.yaml" {{DATA_FILE}} {{file_stem(DATA_FILE)}}

# Jovian devnet
jovian-devnet: (devnet "jovian.yaml")

# Flashblocks devnet
flash-devnet: (devnet "flash.yaml")

# subshells
enter-devnet DEVNET CHAIN='Ethereum' NODE_INDEX='0': _prerequisites
    go run ../devnet-sdk/shell/cmd/enter/main.go --devnet kt://{{DEVNET}} --chain {{CHAIN}} --node-index {{NODE_INDEX}}
